/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package op;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @author Jônatas
 */
public class Config {

    public static final String configFilePath = "proprerties.dat";

    private static void readSingleton() throws IOException {

        if(!new File(configFilePath).exists()) {
            SINGLETON = new Config(null, null);
        }

        BufferedReader br = new BufferedReader(new FileReader(configFilePath));

        String pathChooserPrincipal = null;
        String pathChooserResumo = null;

        String line = "";

        while ((line = br.readLine()) != null) {

            line = line.trim();

            if (line.isEmpty()) {
                continue;
            }

            if (line.startsWith("pathChooserPrincipal")) {
                pathChooserPrincipal = line.split("=")[1];
            } else if (line.startsWith("pathChooserResumo")) {
                pathChooserResumo = line.split("=")[1];
            }

        }

        SINGLETON = new Config(pathChooserPrincipal, pathChooserResumo);

    }
    private String pathChooserPrincipal;
    private String pathChooserResumo;

    public String getPathChooserPrincipal() {
        return pathChooserPrincipal;
    }

    public void setPathChooserPrincipal(String pathChooserPrincipal) {
        this.pathChooserPrincipal = pathChooserPrincipal;
    }

    public String getPathChooserResumo() {
        return pathChooserResumo;
    }

    public void setPathChooserResumo(String pathChooserResumo) {
        this.pathChooserResumo = pathChooserResumo;
    }

    private Config(String pathChooserPrincipal, String pathChooserResumo) {
        this.pathChooserPrincipal = pathChooserPrincipal;
        this.pathChooserResumo = pathChooserResumo;
    }
    private static Config SINGLETON = null;

    public static Config getInstance() throws IOException {
        if (SINGLETON == null) {
            try {
                readSingleton();
            } catch (IOException ex) {
                throw ex;
            }
        }
        return SINGLETON;
    }

    public static void writeConfigs() throws Exception {

        if (SINGLETON == null) {
            throw new IllegalStateException("SINGLETON ainda nao foi instanciada.");
        }

        PrintWriter pw = new PrintWriter(configFilePath);

        pw.println("pathChooserPrincipal=" + SINGLETON.pathChooserPrincipal);
        pw.println("pathChooserResumo=" + SINGLETON.pathChooserResumo);

        pw.close();


    }
}
